home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Media / SharedFx / reflect.fx next >
Encoding:
Text File  |  2004-09-27  |  7.1 KB  |  197 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Flyer.fx
  3. //
  4. // Desc: The effect file for the EffectMesh sample.  The technique implements:
  5. //
  6. //       Texture mapping
  7. //       Diffuse lighting
  8. //       Specular lighting
  9. //       Environment mapping
  10. // 
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //-----------------------------------------------------------------------------
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Global variables
  17. //-----------------------------------------------------------------------------
  18. texture g_txScene;  // texture for scene rendering
  19. texture g_txEnvMap; // texture for environment map
  20.  
  21. shared float4x4 g_mWorldView : WORLDVIEW;               // View matrix for object
  22. shared float4x4 g_mWorldViewProjection : WORLDVIEWPROJECTION; // World * View * Projection matrix
  23. //float4x4 g_mProj;                                       // Projection matrix for object
  24. shared float4x4 g_mViewInv;                             // Inverse of view matrix
  25. shared float4 g_vLightColor = {1.0f, 1.0f, 1.0f, 1.0f}; // Light value
  26. shared float  g_fTime;                                  // Time value
  27. shared float3 g_vLight;                                 // Light position in view space
  28. float  g_fSizeMul = 1.0f;                               // A size multiplier
  29. float  g_fAnimSpeed;                                    // Animation speed
  30.  
  31. // Object material attributes
  32. float4 Diffuse;      // Diffuse color of the material
  33. float4 Specular = {1.0f, 1.0f, 1.0f, 1.0f};  // Specular color of the material
  34. float  Reflectivity; // Reflectivity of the material
  35. float  Power = 1.0f;
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Texture samplers
  40. //-----------------------------------------------------------------------------
  41. sampler g_samScene =
  42. sampler_state
  43. {
  44.     Texture = <g_txScene>;
  45.     MinFilter = Linear;
  46.     MagFilter = Linear;
  47.     MipFilter = None;
  48. };
  49.  
  50. sampler g_samEnv =
  51. sampler_state
  52. {
  53.     Texture = <g_txEnvMap>;
  54.     MipFilter = Linear;
  55.     MinFilter = Linear;
  56.     MagFilter = Linear;
  57. };
  58.  
  59.  
  60. //-----------------------------------------------------------------------------
  61. // Name: VertScene
  62. // Type: Vertex shader
  63. // Desc: This shader computes standard transform and lighting
  64. //-----------------------------------------------------------------------------
  65. void VertScene( float4 vPos : POSITION,
  66.                 float3 vNormal : NORMAL,
  67.                 float2 vTex0 : TEXCOORD0,
  68.                 out float4 oPos : POSITION,
  69.                 out float4 oDiffuse : COLOR0,
  70.                 out float2 oTex0 : TEXCOORD0,
  71.                 out float3 oViewPos : TEXCOORD1,
  72.                 out float3 oViewNormal : TEXCOORD2,
  73.                 out float3 oEnvTex : TEXCOORD3 )
  74. {
  75.     // Transform the position from object space to homogeneous projection space
  76.     oPos = mul( vPos, g_mWorldViewProjection );
  77. //    oPos = mul( vPos, g_mWorldView );
  78. //    oPos = mul( oPos, g_mProj );
  79.  
  80.     // Compute the view-space position
  81.     oViewPos = mul( vPos, g_mWorldView );
  82.  
  83.     // Compute view-space normal
  84.     oViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  85.  
  86.     // Compute lighting
  87.     oDiffuse = dot( oViewNormal, normalize( g_vLight - oViewPos ) ) * Diffuse;
  88.  
  89.     // Just copy the texture coordinate through
  90.     oTex0 = vTex0;
  91.  
  92.     // Compute the texture coord for the environment map.
  93.     oEnvTex = 2 * dot( -oViewPos, oViewNormal ) * oViewNormal + oViewPos;
  94.     oEnvTex = mul( oEnvTex, (float3x3)g_mViewInv );
  95. }
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Name: PixScene
  100. // Type: Pixel shader
  101. // Desc: This shader outputs the pixel's color by modulating the texture's
  102. //         color with diffuse material color
  103. //-----------------------------------------------------------------------------
  104. float4 PixScene( float4 MatDiffuse : COLOR0,
  105.                  float2 Tex0 : TEXCOORD0,
  106.                  float3 ViewPos : TEXCOORD1,
  107.                  float3 ViewNormal : TEXCOORD2,
  108.                  float3 EnvTex : TEXCOORD3 ) : COLOR0
  109. {
  110.     // Compute half vector for specular lighting
  111.     float3 vHalf = normalize( normalize( -ViewPos ) + normalize( g_vLight - ViewPos ) );
  112.  
  113.     // Compute normal dot half for specular light
  114.     float4 fSpecular = pow( saturate( dot( vHalf, normalize( ViewNormal ) ) ) * Specular, Power );
  115.  
  116.     // Lookup mesh texture and modulate it with diffuse
  117.     return float4( (float3)( g_vLightColor * ( tex2D( g_samScene, Tex0 ) * MatDiffuse + fSpecular ) * ( 1 - Reflectivity )
  118.                              + texCUBE( g_samEnv, EnvTex ) * Reflectivity
  119.                            ), 1.0f );
  120. }
  121.  
  122.  
  123. void VertScene1x( float4 vPos : POSITION,
  124.                   float3 vNormal : NORMAL,
  125.                   float2 vTex0 : TEXCOORD0,
  126.                   out float4 oPos : POSITION,
  127.                   out float4 oDiffuse : COLOR0,
  128.                   out float4 oSpecular : COLOR1,
  129.                   out float2 oTex0 : TEXCOORD0,
  130.                   out float3 oEnvTex : TEXCOORD1 )
  131. {
  132.     // Transform the position from object space to homogeneous projection space
  133.     oPos = mul( vPos, g_mWorldViewProjection );
  134.  
  135.     // Compute the view-space position
  136.     float4 ViewPos = mul( vPos, g_mWorldView );
  137.  
  138.     // Compute view-space normal
  139.     float3 ViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldView ) );
  140.  
  141.     // Compute diffuse lighting
  142.     oDiffuse = dot( ViewNormal, normalize( g_vLight - ViewPos ) ) * Diffuse;
  143.  
  144.     // Compute specular lighting
  145.     // Compute half vector
  146.     float3 vHalf = normalize( normalize( -ViewPos ) + normalize( g_vLight - ViewPos ) );
  147.  
  148.     // Compute normal dot half for specular light
  149.     oSpecular = pow( saturate( dot( vHalf, ViewNormal ) ) * Specular, Power );
  150.  
  151.     // Just copy the texture coordinate through
  152.     oTex0 = vTex0;
  153.  
  154.     // Compute the texture coord for the environment map.
  155.     oEnvTex = 2 * dot( -ViewPos, ViewNormal ) * ViewNormal + ViewPos;
  156.     oEnvTex = mul( oEnvTex, (float3x3)g_mViewInv );
  157. }
  158.  
  159.  
  160. float4 PixScene1x( float4 MatDiffuse : COLOR0,
  161.                    float4 MatSpecular : COLOR1,
  162.                    float2 Tex0 : TEXCOORD0,
  163.                    float3 EnvTex : TEXCOORD1 ) : COLOR0
  164. {
  165.     // Lookup mesh texture and modulate it with diffuse
  166.     return ( MatDiffuse * tex2D( g_samScene, Tex0 ) + MatSpecular ) * ( 1 - Reflectivity ) + texCUBE( g_samEnv, EnvTex ) * Reflectivity;
  167. }
  168.  
  169.  
  170. //-----------------------------------------------------------------------------
  171. // Name: RenderScene
  172. // Type: Technique
  173. // Desc: Renders scene to render target
  174. //-----------------------------------------------------------------------------
  175. technique RenderScene
  176. {
  177.     pass P0
  178.     {
  179.         VertexShader = compile vs_1_1 VertScene();
  180.         PixelShader  = compile ps_2_0 PixScene();
  181.         ZEnable = true;
  182.         AlphaBlendEnable = false;
  183.     }
  184. }
  185.  
  186.  
  187. technique RenderScene1x
  188. {
  189.     pass P0
  190.     {
  191.         VertexShader = compile vs_1_1 VertScene1x();
  192.         PixelShader  = compile ps_1_1 PixScene1x();
  193.         ZEnable = true;
  194.         AlphaBlendEnable = false;
  195.     }
  196. }
  197.